package com.qicheng.wlhyspider.anhui.controller;

import com.qicheng.wlhyspider.anhui.dao.AhCompanyRepository;
import com.qicheng.wlhyspider.anhui.entity.AhCompanyEntity;
import com.qicheng.wlhyspider.anhui.service.*;
import com.qicheng.wlhyspider.common.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.CompletableFuture;

/**
 * 安徽企业
 */
@RestController
@RequestMapping("/anhui/company")
@Slf4j
public class AhCompanyController {
    private final AhCompanyRepository ahCompanyRepository;
    private final AhDriverService ahDriverService;
    private final AhVehicleService ahVehicleService;
    private final AhShippingService ahShippingService;
    private final AhFinancialService ahFinancialService;
    private final AhCompanyService ahCompanyService;
    private final RedisUtils redisUtils;

    public AhCompanyController(AhCompanyRepository ahCompanyRepository, AhDriverService ahDriverService, AhVehicleService ahVehicleService, AhShippingService ahShippingService, AhFinancialService ahFinancialService, AhCompanyService ahCompanyService, RedisUtils redisUtils) {
        this.ahCompanyRepository = ahCompanyRepository;
        this.ahDriverService = ahDriverService;
        this.ahVehicleService = ahVehicleService;
        this.ahShippingService = ahShippingService;
        this.ahFinancialService = ahFinancialService;
        this.ahCompanyService = ahCompanyService;
        this.redisUtils = redisUtils;
    }


    @Scheduled(cron = "0/30 * * * * ?")
    public void queryMenu() {
        // 为了刷新token
        HttpUtils.ahHttpGet("https://wlhy.ahyg.com.cn/system/home/queryMenu", redisUtils);
    }

    @GetMapping("/save")
    public Result save(@RequestParam("page") Integer page,
                       @RequestParam("startDate") String startDate,
                       @RequestParam("endDate") String endDate,
                       @RequestHeader("token") String token) {
        try {
            if (StringUtils.isBlank(token)) {
                return Results.error("header里面的token不能为空");
            }
            redisUtils.set(Constants.AH_TOKEN, token);
            String companyId = ahCompanyService.save();
            CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
                Result result = ahDriverService.saveAll(page, startDate, endDate, companyId);
                if (!result.getSuccess()) {
                    throw new RuntimeException(result.getMessage());
                }
            }).thenRunAsync(() -> {
                Result result = ahDriverService.saveDetail();
                if (!result.getSuccess()) {
                    throw new RuntimeException(result.getMessage());
                }
            });
            CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
                Result result = ahVehicleService.saveAll(page, startDate, endDate, companyId);
                if (!result.getSuccess()) {
                    throw new RuntimeException(result.getMessage());
                }
            }).thenRunAsync(() -> {
                Result result = ahVehicleService.saveDetail();
                if (!result.getSuccess()) {
                    throw new RuntimeException(result.getMessage());
                }
            });
            CompletableFuture<Void> future3 = CompletableFuture.runAsync(() -> {
                Result result = ahShippingService.saveAll(page, startDate, endDate, companyId);
                if (!result.getSuccess()) {
                    throw new RuntimeException(result.getMessage());
                }
            }).thenRunAsync(() -> {
                Result result = ahShippingService.saveDetail(companyId);
                if (!result.getSuccess()) {
                    throw new RuntimeException(result.getMessage());
                }
            });
            CompletableFuture<Void> future4 = CompletableFuture.runAsync(() -> {
                Result result = ahFinancialService.saveAll(page, startDate, endDate, companyId);
                if (!result.getSuccess()) {
                    throw new RuntimeException(result.getMessage());
                }
            }).thenRunAsync(() -> {
                Result result = ahFinancialService.saveDetail(companyId);
                if (!result.getSuccess()) {
                    throw new RuntimeException(result.getMessage());
                }
            });

            CompletableFuture.allOf(future1, future2, future3, future4).join();
            return Results.success();
        } catch (Exception e) {
            return Results.error("查询失败:" + e.getMessage());
        }

    }


}
